﻿<?php
/********************************************************
 *
 * @copyright Copyright (c) 2007 - 2008
 * @author 刘昌功 lcg635@126.com
 *
 ********************************************************/
defined('APP_DIR') or die("404 Not Found");

class Common_Model {
	var $_code = '';
	var $_expired = 900;
	var $_tb = null;

	function Common_Model() {
		@session_start();
	}

	/**
     * 隐藏的 hideinput
     *
     * @return array $result
     */
    function verifierInput(){
    	$seed = '346789ABCDEFGHJKLMNPQRTUVWXYabcdefghjklmnpqrtuvwxy';
    	$code = '';
    	$length=4;
    	$lefttime = $this->_expired;
    	for ($i = 0; $i < $length; $i++) {
                $code .=substr( $seed, rand(0, $length), 1);
        }
        $_SESSION['VerifierInput'] = $code;
        $_SESSION['VerifierInput_EXPIRED'] = time() + $lefttime;


    	$this->_ui=FLEA::initWebControls();
		$Input =$this->_ui->control('Textbox', 'hidecode',
   			array(
				'value'=>$code,
				'alt'=>'',
				//'class' => 'myinput',
        		'size' => 10,
       			'maxlength' => 4,
       			//'disable'=>'disable',
       			//readonly='readonly',
    		),
    		true
    	);
    	return $Input;
    }
    /**
     * 校验 验证码的正确性，校验后清除 imgcode
     *
     * @param unknown_type $result
     * @return unknown
     */
    function checkImgcode($codeData)	{
		$imgCode = &FLEA::getSingleton('FLEA_Helper_ImgCode');
		/*@var $imgCode FLEA_Helper_ImgCode */
		if( !$imgCode->check($codeData) ){
			$result = "验证码错误！";
		}
		$imgCode->clear();
		return $result;
	}

	function getUpImages() {
		$files = FLEA::getSingleton('FLEA_Helper_FileUploader');
		$result = array();
		$exts = array('jpg', 'jpeg', 'gif', 'png');
		$savePath = realpath('./upFiles/pic/');
		if ( $files->getCount() > 0 ) {
			$images = $files->getFiles();
			foreach ( $images as $k => $v ) {
				if (!in_array(strtolower($v->getExt()), $exts)) {
					$result['err'][] = $k . '：图片类型只能是“jpg”,“jpeg”,“gif”,“png”中的一种';
					break;
				}
				if ($v->getSize() > 1024000) {
					$result['err'][] = $k . '：图片大小不能超过1M';
					break;
				}
				$fileName = date('YmdHis').rand(1, 1000).'.'.$v->getExt();
				if (!$v->move($savePath.$name.DS.$fileName))
					$result['err'][] = $k . '：图片上传失败';
				else
					$result['image'] .= ',' . $fileName;
			}
		}
		!empty($result['image']) && $result['image'] = substr($result['image'], 1);
		return $result;
	}
	
	//根据existsUserId($id)改写
	//写入数据之前校验是否存在,如：existsRecord('username', 'zero', '用户名已存在', url('users','insert'))
	function existsRecord($field, $value, $alertMsg, $strUrl) {
		if($this->_tb->findCount(array($field => $value)) > 0){
			js_alert($alertMsg, NULL, $strUrl);
		}
		return NULL;
	}
	

}
